package shootingtactics.client.core.bullet;

import java.math.BigDecimal;

import shootingtactics.client.core.enemy.BaseEnemy;

import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;

public class ExplotionBullet extends BaseBullet{

    static CssColor bulletColor = CssColor.make("rgba(255, 0, 0, 1)");

    boolean changeSpeed = false;
    public ExplotionBullet(double posX, double posY, String drawType, Context2d targetContext, BaseEnemy baseEnemy){
        super(posX, posY, drawType, bulletColor, targetContext);
        this.targetEnemy = baseEnemy;
        position.setSize(6, 6);
        baseSpeed = 1;
        setInitSpeed();
    }

    private void setInitSpeed(){
        if(position.getPositionX() - targetEnemy.position.getPositionX() > 0){
            speedX = -baseSpeed;
            speedY = 0;
        }else{
            speedX = baseSpeed;
            speedY = 0;
        }
    }

    @Override
    public void updateSpeed(){
        super.updateSpeed();
        if(!changeSpeed && Math.abs(position.getPositionX() - targetEnemy.position.getPositionX()) < 400){
            baseSpeed = 30;
            double distanceX = targetEnemy.position.getPositionX() - position.getPositionX();
            double distanceY = targetEnemy.position.getPositionY() - position.getPositionY();
            double distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY);

            if(distance != 0){
                speedX = distanceX / distance * baseSpeed;
                BigDecimal round =new BigDecimal(speedX);
                speedX = round.setScale(1, BigDecimal.ROUND_UP).doubleValue();

                speedY = distanceY / distance * baseSpeed;
                round = new BigDecimal(speedY);
                speedY = round.setScale(1, BigDecimal.ROUND_UP).doubleValue();
            }else{
                speedX = baseSpeed;
                speedY = 0;
            }

            changeSpeed = true;
        }
    }
}
